import Request from '@/utils/request/index'
import config from '@/config/index'

export const register = (data) => {
  return Request.request({
    url: '/h5/register',
    method: 'POST',
	data,
  })
}

export const sendCode = (data) => {
  return Request.request({
    url: '/h5/sendCode',
    method: 'POST',
	data,
  })
}

export const login = (data) => {
  return Request.request({
    url: '/h5/account/login',
    method: 'POST',
	data,
  })
}

export const getInfo = (data) => {
  return Request.request({
    url: '/h5/member/info',
    method: 'GET',
	data,
  })
}

export const updateInfo = (data) => {
  return Request.request({
    url: '/h5/certificate/add',
    method: 'POST',
	data,
  })
}

export const uploadAvatar = (filePath) => {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: config.BASE_URL + '/h5/oss/upload',
      filePath: filePath,
      name: 'file',
      header: {
        'Authorization': uni.getStorageSync('token')
      },
      success: (res) => {
        try {
          const data = JSON.parse(res.data)
          if (data.code === 200) {
			resolve(data.url)
          } else {
            reject(new Error(data.msg || this.$t('api_modules_user_msg_54e5de42')))
          }
        } catch (error) {
          reject(new Error(this.$t('api_modules_user_msg_54e5de42')))
        }
      },
      fail: (error) => {
        reject(error)
      }
    })
  })
}

export const getAddressList = (data) => {
  return Request.request({
    url: '/h5/member/address/list',
    method: 'GET',
	data,
  })
}

export const getDefaultAddress = (data) => {
  return Request.request({
    url: '/h5/member/address/default',
    method: 'GET',
	data,
  })
}

export const createAddress = (data) => {
  return Request.request({
    url: '/h5/member/address/create',
    method: 'POST',
	data,
  })
}

export const updateAddress = (data) => {
  return Request.request({
    url: '/h5/member/address/update',
    method: 'PUT',
	data,
  })
}

export const deleteAddress = (id) => {
  return Request.request({
    url: `/h5/member/address/${id}`,
    method: 'DELETE',
  })
}